package com.lg.desireplayer.ranking;


import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lg.desireplayer.R;
import com.lg.desireplayer.bandinfo.ViewBandinfo;
import com.lg.desireplayer.bandlist.Band;
import com.lg.desireplayer.bandlist.BandList;
import com.lg.desireplayer.util.GetImageFromURL;

public class Ranking extends Activity{
	
	private ListView rank;
	private CustomRank totR;
	private ArrayList<Bitmap> images;
	public ArrayList<Band> customRank = new ArrayList<Band>();
	private ProgressBar loading;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
				rank.setAdapter(totR);
				loading.setVisibility(View.GONE);
		}
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranking);
		
		rank = (ListView)findViewById(R.id.Ranking_rank);
		loading = (ProgressBar)findViewById(R.id.Ranking_loading);
		totR = new CustomRank(this);
		images = new ArrayList<Bitmap>();
		Band tmp;  
		
		for(int i=0 ; i<BandList.getSortedBandList().size(); i++) {
			tmp=BandList.getSortedBandList().get(i); 
			customRank.add(i,tmp);
		}    // 전체순위 정렬해서 넣기

		
		BackThread backLoading = new BackThread(mHandler);
		backLoading.setDaemon(true);
		backLoading.start();
		
		
	}
	
	class BackThread extends Thread{
		Handler mhandler;
		BackThread(Handler handler)
		{
			mhandler=handler;
		}
		public void run(){
			for(int i=0; i<BandList.getSize();i++){
				images.add(GetImageFromURL.setImage(customRank.get(i).getId()));
			}
			mhandler.sendEmptyMessage(0);
		}
	}
		
	class CustomRank extends BaseAdapter{
		
		RelativeLayout viewRank; 
		TextView rankNum; 
		ImageView artistImg;
		TextView artistName; 
		TextView artistSong;
		Bitmap bandImg;
		
	
		Intent intent;
				
		
		private Activity mContext;
		public CustomRank(Activity context) {
			mContext=context;
			//
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return customRank.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			View row = convertView;
			
			if(row == null)
			{
				LayoutInflater inflater = mContext.getLayoutInflater();
				row = inflater.inflate(R.layout.ranking_costom, null);
				
			}
			viewRank = (RelativeLayout)row.findViewById(R.id.raking_customView);
			rankNum = (TextView)row.findViewById(R.id.ranking_cutrom_rankNum);
			artistImg = (ImageView)row.findViewById(R.id.ranking_cutrom_artistPotho);
			artistName = (TextView)row.findViewById(R.id.ranking_cutrom_artist);
			artistSong = (TextView)row.findViewById(R.id.ranking_cutrom_song);
			
			//bandImg=GetImageFromURL.setImage(customRank.get(position).getId());
			
			artistImg.setImageBitmap(images.get(position));
			artistName.setText(customRank.get(position).getName());
			artistSong.setText(customRank.get(position).getTitle());
		
			ImageButton bandInfo = (ImageButton)row.findViewById(R.id.ranking_cutrom_infoBtn);
			// 이미지 버튼으로 밴드 정보 받아오기
			
			if(position%2==0) // 홀수 짝수 순위 배경색 다르게 하기
			{
				viewRank.setBackgroundColor(Color.parseColor("#E4E4E4"));		
			}
			else
			{
				viewRank.setBackgroundColor(Color.parseColor("#EBF7FF"));
			}
			rankNum.setText(Integer.toString(position+1));
			
			bandInfo.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					intent = new Intent(Ranking.this, ViewBandinfo.class);
					intent.putExtra("id", customRank.get(position).getId());
					
					startActivity(intent);
				}
			});
			
			
			return row;
		}
}
	
}
